package com.example.basic01;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Date 2022/5/21
 * @Created by Jonathan
 */
@Slf4j
public class MapDemo {
    static class Stu {
        private String name;
        private Integer id;

        public Stu(String name, Integer id) {
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public Integer getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Stu{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    '}';
        }
    }

    public static void main(String[] args) {

        List<Stu> list = new ArrayList<>();
        list.add(new Stu("张三", 1));
        list.add(new Stu("李四", 2));
        list.add(new Stu("赵六", 3));
        log.info("list");
        // 测试 forEach
        List<String> collect = Stream.of("1", "2", "3")
                .collect(Collectors.toList());
        list.forEach(o -> {
            if (o.getId() <= 1) {
                return;
            }
            System.out.println(o);
        });
        final Map<Integer, Stu> stuMap = list.stream()
                .filter(o -> o.getId() >= 2)
                .filter(o -> o.getId() >= 3)
                .collect(Collectors.toMap(Stu::getId, o -> o));
        System.out.println(stuMap);
    }
}
